﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Security.Permissions;
using System.Threading;

namespace IRC.IRepairClouds.DataLayer
{
    partial class MultiKeyDictionary<TKey, TValue>
    {
        [Serializable]
        public struct Enumerator
            : IEnumerator<MultiKeyValuePair<TKey, TValue>>,
            IDisposable,
            //IDictionaryEnumerator, 
            IEnumerator
        {
            internal const int DictEntry = 1;
            internal const int KeyValuePair = 2;
            private MultiKeyDictionary<TKey, TValue> dictionary;
            private int version;
            private int index;
            private MultiKeyValuePair<TKey, TValue> current;
            private int getEnumeratorRetType;
            internal Enumerator(MultiKeyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType)
            {
                this.dictionary = dictionary;
                this.version = dictionary.version;
                this.index = 0;
                this.getEnumeratorRetType = getEnumeratorRetType;
                this.current = new MultiKeyValuePair<TKey, TValue>();
            }

            public bool MoveNext()
            {
                if (this.version != this.dictionary.version) {
                    throw new InvalidOperationException();
                    //ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                }
                while (this.index < this.dictionary.valCount) {
                    if (this.dictionary.valEntries[this.index].valid) {
                        this.current = new MultiKeyValuePair<TKey, TValue>(this.dictionary.valEntries[this.index].allKeys, this.dictionary.valEntries[this.index].value);
                        this.index++;
                        return true;
                    }
                    this.index++;
                }
                this.index = this.dictionary.valCount + 1;
                this.current = new MultiKeyValuePair<TKey, TValue>();
                return false;
            }

            public MultiKeyValuePair<TKey, TValue> Current
            {
                get
                {
                    return this.current;
                }
            }
            public void Dispose()
            {
            }

            object IEnumerator.Current
            {
                get
                {
                    if ((this.index == 0) || (this.index == (this.dictionary.valCount + 1))) {
                        throw new InvalidOperationException();
                        //ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
                    }
                    //if (this.getEnumeratorRetType == 1) {
                    //    return new DictionaryEntry(this.current.Key1, this.current.Key2, this.current.Value);
                    //}
                    return new MultiKeyValuePair<TKey, TValue>(this.current.Keys, this.current.Value);
                }
            }
            void IEnumerator.Reset()
            {
                if (this.version != this.dictionary.version) {
                    throw new InvalidOperationException();
                    //ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
                }
                this.index = 0;
                this.current = new MultiKeyValuePair<TKey, TValue>();
            }

            //DictionaryEntry IDictionaryEnumerator.Entry
            //{
            //    get
            //    {
            //        if ((this.index == 0) || (this.index == (this.dictionary.count + 1))) {
            //            throw new InvalidOperationException();
            //            //ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
            //        }
            //        return new DictionaryEntry(this.current.Key, this.current.Value);
            //    }
            //}
            //object IDictionaryEnumerator.Key
            //{
            //    get
            //    {
            //        if ((this.index == 0) || (this.index == (this.dictionary.count + 1))) {
            //            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
            //        }
            //        return this.current.Key;
            //    }
            //}
            //object IDictionaryEnumerator.Value
            //{
            //    get
            //    {
            //        if ((this.index == 0) || (this.index == (this.dictionary.count + 1))) {
            //            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
            //        }
            //        return this.current.Value;
            //    }
            //}
        }
    }
}
